<%--
  Created by IntelliJ IDEA.
  User: liuxi
  Date: 2018/6/13
  Time: 11:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/bootstrap/css/bootstrap.css"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/table/bootstrap-table.min.css"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/date/bootstrap-datetimepicker.min.css"/>
    <link rel="stylesheet" type="text/css"
          href="${pageContext.request.contextPath}/statics/sweetalert.css"/>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/statics/sweetalert.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/statics/jquery-3.2.1.min.js"></script>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/statics/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/statics/table/bootstrap-table.min.js"></script>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/statics/table/locale/bootstrap-table-zh-CN.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/statics/date/Moment.js"></script>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/statics/date/bootstrap-datetimepicker.min.js"></script>
</head>
<body>
<div class="container" style="margin-top: 50px">
    <table id="tb"></table>
</div>
<%--分配角色模态框--%>
<div id="myModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form>
                <h3>分配权限</h3>
                <input type="hidden" name="posId" id="posId"/>
                <ul id="ul">

                </ul>

                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary add" onclick="grantRole()">分配</button>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</body>
<script type="text/javascript">
    var roleid;
    window.operateEvents = {
        "click .distributionRole": function (e, value, row, index) {
            var posId = row.posId;
            roleid = row.posId;
            $("#posId").val(row.posId);
            $("#ul").empty() ;
            $.ajax({
                "url":'${pageContext.request.contextPath}/Permission/Jurisdiction',
                "data":{"PosId":posId},
                "type":"post",
                "dataType":"json",
                "success":function (data) {
                    console.log(data.length);
                    var s = "" ;
                    for(var i=0;i<data.length;i++){
                        //每一个Permission
                        var obj = data[i] ;
                        var type = obj.rType ;
                        if(type=='menu') {
                            s = s + "<li> " + obj.rName + "</li>";
                            s = s + "<ul>";
                            for(var j=0;j<data.length;j++){
                                //每一个permission
                                var obj1 = data[j] ;
                                if(obj1.rType=='permission'&&obj.rId==obj1.rPercode){
                                    if(obj1.isselect==1) {
                                        s = s + "<li><input class='xx"+obj.rId+"' checked type='checkbox' name='pername' value='" + obj1.rId + "' /> " + obj1.rName + "</li>"
                                    } else{
                                        s = s + "<li><input class='xx"+obj.rId+"'  type='checkbox' name='pername' value='" + obj1.rId + "' /> " + obj1.rName + "</li>"
                                    }
                                }
                            }
                            s = s + "</ul>";
                        }
                    }
                    $(s).appendTo("#ul") ;
                    $("#myModal").modal('show') ;
                }
            });
            $("#myModal").modal('show');
        }
    };
    function grantRole(){
        var s = "" ;
        var arr = $("input[name='pername']") ;
        for(var i=0;i<arr.length;i++){
            var ck = arr[i] ;
            if(ck.checked){
                s = s + ck.value + "-" ;
            }
        }
        if(s.length==0){
            s = "asd";
        }
        $.post("${pageContext.request.contextPath}/Permission/doGrant",{'permissionids' : s,'roleid':roleid},function(data){
            if(data=='ok'){
                $("#myModal").modal('hide') ;
                $("#tb").bootstrapTable('refresh');
            }
        }) ;
    }
    $(function(){
        //bootstrapTable
        $("#tb").bootstrapTable({
            url: '${pageContext.request.contextPath}/Nurse/queryPosition',
            columns: [{
                field: 'posId',
                title: '编号'
            }, {
                field: 'posName',
                title: '角色'
            }, {
                title: '操作',
                events: operateEvents,
                formatter: caozuo
            }],
            method: 'post',
            contentType: "application/x-www-form-urlencoded",
            height: 500,
            pageList: [5, 10, 15],
            pageSize: 5,
            sortable: true,
            sortOrder: "asc",
            pageNumber: 1,
            pagination: true
        });
        function caozuo(value, row, index) {
            return [
                "<a class='distributionRole' href=javascript:del()>分配权限</a>"
            ].join("");
        }

    });
</script>
</html>
